import Vue from 'vue'

import { ACCESS_TOKEN, RRFRESH_TOKEN } from '@/store/mutation-types'

/**
 * token
 */
export function getToken(refresh) {
  return refresh ? Vue.ls.get(RRFRESH_TOKEN) : Vue.ls.get(ACCESS_TOKEN)
}
// ===============  ================= //
/**
 * 带上bearer头
 */
export function getRequestToken(refresh) {
  return 'evildog' + getToken(refresh)
}

/**
 * 设置本地缓存，不考虑设置过期
 *
 * accessTokenExpire 单位s 考虑到网络延时，将accessToken的过期时间设置的比实际短一点
 * 便于permission.js可以实时监测到token不存在
 */
export function setToken(accessToken, refreshToken, accessTokenExpire, refreshTokenExpire) {
  Vue.ls.set(ACCESS_TOKEN, accessToken, (accessTokenExpire - 1000) * 1000)
  Vue.ls.set(RRFRESH_TOKEN, refreshToken, (refreshTokenExpire - 1000) * 1000)
}

export function removeToken() {
  Vue.ls.remove(ACCESS_TOKEN)
  Vue.ls.remove(RRFRESH_TOKEN)
}
